<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>收货地址管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 地址列表区域 -->
      <el-table border stripe :data="addressList" style="width: 100%">
        <el-table-column type="index"></el-table-column>
        <!-- <el-table-column prop="id" label="#"></el-table-column> -->
        <el-table-column prop="address" label="地址"> </el-table-column>
        <el-table-column label="操作" width="250px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="showEditDialog(scope.row.id)">修改地址</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="removeAddressById(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row :gutter="10" class="roleStyle">
        <el-col :offset="20">
          <el-button type="primary" @click="showAddaddress">添加收货地址</el-button>
        </el-col>
      </el-row>
    </el-card>
    <!-- 修改地址的对话框 -->
    <el-dialog title="修改地址" :visible.sync="editDialogVisible" width="50%">
      <el-form :model="addressList" ref="roleFormRef" label-width="100px">
        <el-form-item label="旧地址" prop="oldAddress">
          <el-input v-model="oldAddress" disabled></el-input>
        </el-form-item>
        <el-form-item label="新地址" prop="newAddress">
          <el-input v-model="newAddress"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureEditAddress()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加收货地址的对话框 -->
    <el-dialog title="添加收货地址" :visible.sync="addAddressVisible" width="50%" @close="addAddressClosed">
      <el-form :model="addNewAddress" ref="addAddressRef" label-width="100px">
        <el-form-item label="新增地址">
          <el-input v-model="addNewAddress" placeholder="输入地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addAddressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAddress()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      addressList: [],
      addressInfo: {},
      total: 0,
      
      editDialogVisible: false,
      // 控制添加用户对话框的显示与隐藏
      addAddressVisible: false,
      // 控制修改用户对话框的显示与隐藏
      editDialogVisible: false,
      // 旧地址
      oldAddreaa: '',
      // 新地址
      newAddress: '',
      // 新增地址
      addNewAddress:''
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    // 更新地址列表
    async getUserList() {
      const { data: res } = await this.$http.get('address')
      if (res.status !== 200) {
        return this.$message.error('获取用户列表失败！')
      }
      this.addressList = res.data.result
    },
    // 监听修改用户对话框的关闭事件
    addAddressClosed() {
      this.$refs.addAddressRef.resetFields()
    },
    // 删除地址信息
    async removeAddressById(row) {
      console.log('row', row.id)
      // 弹框提示用户是否要删除
      const confirmResult = await this.$confirm('此操作将永久删除该地址, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch((err) => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('取消了删除！')
      }
      const { data: res } = await this.$http.delete(`address/${row.id}`)

      if (res.status !== 200) {
        return this.$message.error('删除地址失败！')
      }
      this.getUserList()
    },
    // 展示修改用户地址的对话框
    async showEditDialog(id) {
      this.addressInfo.id = id
      const { data: res } = await this.$http.post('address/id', { id })
      this.addressInfo.address = res.data
      if (res.status !== 200) {
        return this.$message.error('获取地址失败！')
      }
      this.oldAddress = res.data
      this.$message.success('获取地址成功！')
      this.editDialogVisible = true
    },
    async sureEditAddress() {
      const { data: res } = await this.$http.put('address', {
        id: this.addressInfo.id,
        address: this.newAddress,
      })
      if (res.status !== 200) {
        return this.$message.error('修改地址失败！')
      }
      this.$message.success('修改地址成功！')
      this.getUserList()
      this.editDialogVisible = false
    },
    // 显示添加地址对话框
    showAddaddress() {
      this.addAddressVisible = true
    },
    async addAddress() {
      let address = this.addNewAddress
      const {data:res} = await this.$http.post('address',{address})
      if(res.status!==201) {
        return this.$message.error('添加地址失败！')
      }
      this.$message.success('添加地址成功')
      this.getUserList()
      this.addAddressVisible = false
    }
  },
}
</script>

<style lang="less" scoped>
.roleStyle {
  margin-top: 20px;
}
</style>
